﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;

namespace ZG_member
{
    public partial class MemberRecord : Form
    {
        public MemberRecord()
        {
            InitializeComponent();
            this.Opacity = 0;
            skinEngine1.SkinFile = Application.StartupPath + ConfigurationManager.AppSettings["Skin"];
        }

        public int pageNum=1;
        public int pageSize=12;
        public string strWhere = "";
        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();
            }

        }

        public int GetPageCount(DataTable dt)
        {
            int yushu=  dt.Rows.Count % pageSize;

            if (yushu != 0)
            {
                int countPage = (dt.Rows.Count / pageSize) + 1;
                return countPage;
            }
            else
            {
                return dt.Rows.Count / pageSize;
            }
        }

        public void dgv()
        {
            ZG_member.BLL.memberInfo member=new ZG_member.BLL.memberInfo();
            ZG_member.Model.memberInfo memberMod = new ZG_member.Model.memberInfo();
            ZG_member.BLL.goods goods = new ZG_member.BLL.goods();
            ZG_member.Model.goods goodsMod = new ZG_member.Model.goods();
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                string memberid = dataGridView1.Rows[i].Cells["memberid"].Value.ToString();
                memberMod = member.GetModels(memberid);
                if (memberMod == null)
                {
                    dataGridView1.Rows[i].Cells["memberName"].Value = "未找到该会员";
                }
                else
                {
                    dataGridView1.Rows[i].Cells["memberName"].Value = memberMod.memberName;
                }
                string goodid = dataGridView1.Rows[i].Cells["goodid"].Value.ToString();
                goodsMod=goods.getModelGoodsIdString(goodid);
                if (goodsMod == null)
                {
                    dataGridView1.Rows[i].Cells["goodsName"].Value = "未找到该商品";
                }
                else
                {
                    dataGridView1.Rows[i].Cells["goodsName"].Value = goodsMod.goodsName;
                }
            }
        }

        private void MemberRecord_Load(object sender, EventArgs e)
        {
            timer1.Start();
            ZG_member.BLL.taocanliushuiBLL taocan=new ZG_member.BLL.taocanliushuiBLL();
            dataGridView1.DataSource= taocan.GetAllPage(pageNum, pageSize,strWhere).Tables[0];
            page2.Text= GetPageCount(taocan.GetAll().Tables[0]).ToString();
            dgv();
            
        }
        int flag = 0;
        private void MemberRecord_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
        }

        private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (Convert.ToInt32(page1.Text) < Convert.ToInt32(page2.Text))
            {
                pageNum++;
                ZG_member.BLL.taocanliushuiBLL taocan = new ZG_member.BLL.taocanliushuiBLL();
                dataGridView1.DataSource = taocan.GetAllPage(pageNum, pageSize, strWhere).Tables[0];
                page1.Text = pageNum.ToString();
                dgv();
            }

        }

        private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (Convert.ToInt32(page1.Text) > 1)
            {
                pageNum--;
                ZG_member.BLL.taocanliushuiBLL taocan = new ZG_member.BLL.taocanliushuiBLL();
                dataGridView1.DataSource = taocan.GetAllPage(pageNum, pageSize, strWhere).Tables[0];
                page1.Text = pageNum.ToString();
                dgv();
            }
        }

        private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            
                pageNum=Convert.ToInt32(page2.Text);
                ZG_member.BLL.taocanliushuiBLL taocan = new ZG_member.BLL.taocanliushuiBLL();
                dataGridView1.DataSource = taocan.GetAllPage(pageNum, pageSize, strWhere).Tables[0];
                page1.Text = pageNum.ToString();
                dgv();
            
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            pageNum = 1;
            ZG_member.BLL.taocanliushuiBLL taocan = new ZG_member.BLL.taocanliushuiBLL();
            dataGridView1.DataSource = taocan.GetAllPage(pageNum, pageSize, strWhere).Tables[0];
            page1.Text = pageNum.ToString();
            dgv();
        }

        private void linkLabel5_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            
                pageNum=Convert.ToInt32(textBox1.Text);
                ZG_member.BLL.taocanliushuiBLL taocan = new ZG_member.BLL.taocanliushuiBLL();
                dataGridView1.DataSource = taocan.GetAllPage(pageNum, pageSize, strWhere).Tables[0];
                page1.Text = pageNum.ToString();
                dgv();
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ZG_member.BLL.memberInfo member = new ZG_member.BLL.memberInfo();
            ZG_member.BLL.taocanliushuiBLL taocan = new ZG_member.BLL.taocanliushuiBLL();
            strWhere += " time >='"+dateTimePicker1.Text+"' and time<='"+dateTimePicker2.Text+"' ";
            if (textBox2.Text.Trim() != "")
            {
                DataTable dt = member.GetList(" memberName='" + textBox2.Text + "' ").Tables[0];
                for (int i = 0; i <dt.Rows.Count ; i++)
                {
                   strWhere += " and memberid='" + dt.Rows[i]["memberId"].ToString() + "' ";
                }

            }
            pageNum = 1;
            page1.Text = pageNum.ToString();
            DataTable dt2 = taocan.GetAllPage(pageNum, pageSize, strWhere).Tables[0];
            page2.Text = GetPageCount(dt2).ToString();
            dataGridView1.DataSource = dt2;
            dgv();

        }
    }
}
